/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.ui;

import ac.ui.controls.PCLButton;
import java.awt.event.KeyEvent;

/**
 * The basic class template for the windows in the project, it has all the necessary 
 * functions for the subclasses to implement and customize.
 * @author empcl_000
 */
public class FrameOperationTemplat extends ac.ui.FrameOpertion {

    public FrameOperationTemplat() {
    }

    /**
     * Set the print template with the template in the passed file.
     * @param fileurl URL of the file with the desired template
     */
    public void setPrint(String fileurl) {

        addprintTemp(fileurl);

    }
/**
 * Creates a report using the passed SQL query and the template in the passed file URL.
 * @param fileurl URl of the file with the report template
 * @param q the query to execute and display in the report.
 */
    public void setPrint(String fileurl, String q) {

        ac.reportes.reportes r = new ac.reportes.reportes();
        r.tabel = TabelString;
        r.Name = TabelString + "_file_print";
        r.Query = q;
        r.craeteReporteFromQuery();
        r.showReport(fileurl);

    }

    //<editor-fold defaultstate="collapsed" desc="permissions">
    ac.lib.users.permissions per = new ac.lib.users.permissions();
    public PCLButton perBT = null;
    public String perActionType = "";
    public boolean perStatus = false;

    public boolean perMessage = true;
/**
 * Reset the permission status
 */
    void retstatus() {
        perBT = null;
        perActionType = "";
        perStatus = false;

        perMessage = true;

    }
/**
 * Checks if the user has a permission to invoke a certain button action.
 * @param type the button type
 * @return true if the user has the permission and false otherwise.
 */
    boolean checkPer(String type) {

        PCLButton bt = (PCLButton) ActionsButtons.get(type);
        if (perBT != null) {
            bt = perBT;
        }
        String perAction = "";
        if (bt != null) {
            perAction = bt.getType();
        }
        if (!perActionType.equalsIgnoreCase("")) {
            perAction = perActionType;
        }

        boolean returnStatus = false;

        if (perStatus != false) {
            returnStatus = perStatus;
        } else {
            returnStatus = per.isCan(perAction, perMessage);
        }
        retstatus();
        return returnStatus;

    }

    public boolean checkPerType(String type) {

        this.perActionType = type;

        return checkPer("");
    }

    public boolean checkpricSell() {
        return checkPerType("SellPricC");
    }

    public boolean checkpricBay() {
        return checkPerType("bayPricC");

    }

    public boolean checkPerAdd() {

        return checkPer("add");

    }

    public boolean checkPerEdit() {

        return checkPer("edit");

    }

    public boolean checkPerDelete() {

        return checkPer("delete");

    }

    public boolean checkPerPrint() {
        return checkPer("print");

    }
//</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Add">
    /**
     * 
     * @param status
     * @param updateindex
     * @param updatecoms
     * @param statusEnableAfter
     * @return 
     */
    public int setnewRetuenSataus(boolean status, boolean updateindex, boolean updatecoms, Boolean statusEnableAfter) {

        int r = 0;
        if (checkPerAdd()) {
            r = addnewTempr(status, statusEnableAfter, true);

            if (actionStatus == 0) {
                if (updateindex) {

                    indexUpdate();
                }
                if (updatecoms) {

                    FrameDataupdate("frist");

                }
            }
        }

        return r;
    }
/**
 * 
 * @param status
 * @param updateindex
 * @param updatecoms
 * @param statusEnableAfter
 * @param clear
 * @return 
 */
    public int setnewRetuenSataus(boolean status, boolean updateindex, boolean updatecoms, Boolean statusEnableAfter, Boolean clear) {

        int r = 0;
        if (checkPerAdd()) {
            r = addnewTempr(status, statusEnableAfter, clear);

            if (actionStatus == 0) {
                if (updateindex) {

                    indexUpdate();
                }
                if (updatecoms) {

                    FrameDataupdate("frist");

                }
            }
        }

        return r;
    }
/**
 * 
 * @param status
 * @param updateindex
 * @param updatecoms
 * @return 
 */
    public int setnewRetuenSataus(boolean status, boolean updateindex, boolean updatecoms) {

        int r = 0;
        if (checkPerAdd()) {
            r = addnewTempr(status, false, true);

            if (actionStatus == 0) {
                if (updateindex) {

                    indexUpdate();
                }
                if (updatecoms) {

                    FrameDataupdate("frist");

                }
            }
        }
        return r;
    }
/**
 * 
 * @param status
 * @param updateindex
 * @param updatecoms
 * @return 
 */
    public String setnew(boolean status, boolean updateindex, boolean updatecoms) {

        String r = "";
        if (checkPerAdd()) {

            addnewTemp(status, false);

            if (actionStatus == 0) {
                if (updateindex) {

                    indexUpdate();
                }
                if (updatecoms) {

                    r = FrameDataupdate("frist");

                }
            }
        }
        return r;

    }
/**
 * 
 * @param status
 * @param updateindex
 * @param updatecoms
 * @param statusEnableAfter
 * @return 
 */
    public String setnew(boolean status, boolean updateindex, boolean updatecoms, Boolean statusEnableAfter) {

        String r = "";
        if (checkPerAdd()) {
            addnewTemp(status, statusEnableAfter);

            if (actionStatus == 0) {
                if (updateindex) {

                    indexUpdate();
                }
                if (updatecoms) {

                    r = FrameDataupdate("frist");

                }
            }
        }
        return r;
    }
//</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="edit">
/**
 * 
 * @param status
 * @param updateindex
 * @param updatecoms
 * @return 
 */
    public String setedit(boolean status, boolean updateindex, boolean updatecoms) {
        String r = "";

        if (checkPerEdit()) {
            addEditTemp(status, false);

            if (actionStatus == 0) {
                if (updateindex) {
                    indexUpdate();
                }
                if (updatecoms) {
                    r = FrameDataupdate(IndexID - 1);
                }
            } else {
                dataComponents[0].requestFocus();

            }
        }
        return r;

    }
/**
 * 
 * @param status
 * @param updateindex
 * @param updatecoms
 * @return 
 */
    public int seteditRetuenSataus(boolean status, boolean updateindex, boolean updatecoms) {
        int r = 0;
        if (checkPerEdit()) {
            r = addEditTemp(status, false);

            if (actionStatus == 0) {
                if (updateindex) {
                    indexUpdate();
                }
                if (updatecoms) {
                    FrameDataupdate(IndexID - 1);
                }
            } else {
                dataComponents[0].requestFocus();

            }
        }
        return r;

    }
/**
 * 
 * @param status
 * @param updateindex
 * @param updatecoms
 * @param statusEnableAfter
 * @return 
 */
    public String setedit(boolean status, boolean updateindex, boolean updatecoms, boolean statusEnableAfter) {
        String r = "";
        if (checkPerEdit()) {
            addEditTemp(status, statusEnableAfter);

            if (actionStatus == 0) {
                if (updateindex) {
                    indexUpdate();
                }
                if (updatecoms) {
                    r = FrameDataupdate(IndexID - 1);
                }
            } else {
                dataComponents[0].requestFocus();

            }
        }
        return r;

    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="delete">
    /**
     * 
     * @param status
     * @param updateindex
     * @param updatecoms
     * @return 
     */
    public int setdeleteStatus(boolean status, boolean updateindex, boolean updatecoms) {

        int r = 0;
        if (checkPerDelete()) {
            r = addDeletTemp(status);

            if (actionStatus == 0) {
                if (updateindex) {

                    indexUpdate();
                }
                if (updatecoms) {

                    FrameDataupdate("frist");

                }
            }
        }
        return r;
    }
/**
 * 
 * @param status
 * @param updateindex
 * @param updatecoms
 * @return 
 */
    public String setdelete(boolean status, boolean updateindex, boolean updatecoms) {

        String r = "";

        if (checkPerDelete()) {
            addDeletTemp(status);

            if (actionStatus == 0) {
                if (updateindex) {

                    indexUpdate();
                }
                if (updatecoms) {

                    r = FrameDataupdate("frist");

                }
            }
        }
        return r;
    }
/**
 * 
 * @param where
 * @param status
 * @param updateindex
 * @param updatecoms
 * @return 
 */
    public String setdelete(String where, boolean status, boolean updateindex, boolean updatecoms) {

        String r = "";
        if (checkPerDelete()) {
            addDeletTemp(status, where);

            if (actionStatus == 0) {
                if (updateindex) {

                    indexUpdate();
                }
                if (updatecoms) {

                    r = FrameDataupdate("frist");

                }
            }
        }
        return r;
    }
//</editor-fold>

    /**
     * Handles the keyboard events.
     * @param e keyboard event object encapsulating the key pressed
     * @return the outcome of the operation according to the key pressed.
     */
    public String getkeyPressed(KeyEvent e) {
        String r = "";
        if (e.getKeyCode() == KeyEvent.VK_CANCEL) {
            r = setcancel();
        } else if (e.getKeyCode() == KeyEvent.VK_DELETE) {
            r = setdelete(true, true, true);
        } else if (e.getKeyCode() == KeyEvent.VK_F6) {
            //  r = setnew(null,true, true, true);
        } else if (e.getKeyCode() == KeyEvent.VK_F7) {
            r = setedit(true, true, true);
        }
        return r;
    }
}
